package cn.gz3c.uitest.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.huawei.hms.audioeditor.sdk.asset.HAEAsset;
import com.huawei.hms.audioeditor.sdk.effect.HAEEffect;
import com.huawei.hms.audioeditor.sdk.lane.HAEAudioLane;
import com.huawei.hms.audioeditor.ui.editor.trackview.view.MainHorizontalScrollView;

import java.lang.ref.WeakReference;

import cn.gz3c.uitest.Util;
import cn.gz3c.uitest.viewmodel.EditPreviewViewModel;


public abstract class BaseTrackView extends View {
    public int a;
    protected int b;
    protected boolean c;
    private float d;
    private int e;
    protected int f;
    protected long g;
    private Paint h;
    private Paint i;
    private int j;
    private float k;
    protected int l;
    protected double m;
    protected double n;
    protected double o;
    private double p;
    protected long startTime;
    protected long r;
    protected long s;
    protected EditPreviewViewModel editPreviewViewModel;
    private Activity u;
    private String v;
    private HAEEffect w;
    private HAEAsset x;
    private String y;
    protected double z;
    private float A;
    protected int B;
    protected int C;
    protected long D;
    protected int E;
    protected int F;
    protected WeakReference<EditPreviewViewModel> G;
    protected final int H;

    public BaseTrackView(Activity var1, EditPreviewViewModel var2) {
        super(var1);
        BaseTrackView var10000 = this;
        BaseTrackView var10001 = this;
        this.a = com.huawei.hms.audioeditor.ui.common.utils.a.a(18.0F);
        this.b = com.huawei.hms.audioeditor.ui.common.utils.a.a(9.0F);
        this.c = false;
        this.d = (float) com.huawei.hms.audioeditor.ui.common.utils.a.a(3.0F);
        this.e = com.huawei.hms.audioeditor.ui.common.utils.a.a(140.0F);
        this.f = com.huawei.hms.audioeditor.ui.common.utils.a.a(2.0F);
        this.g = 0L;
        this.j = -1;
        this.k = 0.0F;
        this.l = 1;
        this.m = (double) com.huawei.hms.audioeditor.ui.common.utils.a.a(120.0F);
        this.p = 1000.0D;
        this.startTime = 0L;
        this.r = 0L;
        this.s = 0L;
        this.B = -1;
        this.C = -16777216;
        this.D = 0L;
        this.E = com.huawei.hms.audioeditor.ui.common.utils.a.a(3.0F);
        this.F = -1;
        this.G = new WeakReference<>(var2);
        this.u = var1;
        Rect var3;
        Rect var10002 = var3 = new Rect();
        var1.getWindowManager().getDefaultDisplay().getRectSize(var3);
        H = var10002.right - var3.left;
        var10000.q();
    }

    @SuppressLint({"MissingPermission", "WrongConstant"})
    private void u() {
        Vibrator var1;
        if (com.huawei.hms.audioeditor.ui.common.utils.a.a(this.u, new String[]{"android.permission.VIBRATE"}) && (var1 = (Vibrator) this.u.getSystemService("vibrator")) != null) {
            var1.vibrate(80L);
        }

    }

    private float b(MotionEvent var1) {
        float var10000 = var1.getX();
        //SmartLog.i("BaseTrackView", "calculation x1: " + var10000);
        return var10000;
    }

    protected void b(int var1) {
        EditPreviewViewModel var2;
        if ((var2 = this.editPreviewViewModel) != null && var2.G() != null) {
            this.setMeasuredDimension((int) com.huawei.hms.audioeditor.ui.common.utils.a.d(com.huawei.hms.audioeditor.ui.common.utils.a.b((double) this.editPreviewViewModel.G().getDuration(), com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l)) + (double) com.huawei.hms.audioeditor.ui.common.utils.a.a(this.u), this.m), var1);
        }
    }

    @SuppressLint({"DrawAllocation"})
    protected void onDraw(Canvas var1) {
        super.onDraw(var1);
        if (!(this instanceof WaveTrackWithFootPrintsView)) {
            if (this.o().equals(this.editPreviewViewModel.getLdUuid().getValue())) {
                Canvas var10000 = var1;
                Canvas var10001 = var1;
                Canvas var10002 = var1;
                Canvas var10003 = var1;
                Canvas var10004 = var1;
                Canvas var10005 = var1;
                Canvas var10006 = var1;
                double var2 = (double) this.m();
                float var10 = (float) (this.j() + var2 + (double) (this.a * 2) + (double) com.huawei.hms.audioeditor.ui.common.utils.a.a(2.0F));
                int var11 = this.m() - com.huawei.hms.audioeditor.ui.common.utils.a.a(1.0F);
                float var3 = (float) this.getMeasuredHeight();
                float var4 = (float) com.huawei.hms.audioeditor.ui.common.utils.a.a(1.0F);
                float var5 = (float) (com.huawei.hms.audioeditor.ui.common.utils.a.a(10.0F) + this.m());
                float var6 = (float) com.huawei.hms.audioeditor.ui.common.utils.a.a(10.0F);
                RectF var10007 = new RectF((float) var11, 0.0F, (float) (var11 + this.a), var3);
                Paint var7 = this.h;
                var10006.drawRoundRect(var10007, 20.0F, 20.0F, var7);
                RectF var9 = new RectF(var10 - (float) this.a, 0.0F, var10, var3);
                var7 = this.h;
                var10005.drawRoundRect(var9, 20.0F, 20.0F, var7);
                int var10008 = var11 + this.b;
                int var13 = var11 + this.a;
                int var8 = (int) var3;
                var10004.drawRect(new Rect(var10008, 0, var13, var8), this.h);
                var10003.drawRect(new Rect((int) (var10 - (float) this.a), 0, (int) (var10 - (float) this.b), var8), this.h);
                Paint var14 = var7 = new Paint();
                var7.setStyle(Style.STROKE);
                var7.setStrokeWidth((float) com.huawei.hms.audioeditor.ui.common.utils.a.a(2.0F));
                var14.setColor(this.B);
                var10002.drawRect(new Rect(var11 + this.a, com.huawei.hms.audioeditor.ui.common.utils.a.a(1.0F) - 1, (int) (var10 - (float) this.a), var8 - com.huawei.hms.audioeditor.ui.common.utils.a.a(1.0F) + 1), var7);
                float var12;
                var10001.drawRoundRect(new RectF(var5, (var3 - (float) (var11 = this.e)) / 2.0F, var5 + this.d, (var3 + (float) var11) / 2.0F), (var12 = this.d) / 2.0F, var12 / 2.0F, this.i);
                var10000.drawRoundRect(new RectF((var10 -= var6) - this.d - var4, (var3 - (float) (var11 = this.e)) / 2.0F, var10 - var4, (var3 + (float) var11) / 2.0F), (var10 = this.d) / 2.0F, var10 / 2.0F, this.i);
            }

        }
    }

    protected void q() {
        this.h = new Paint();
        this.h.setColor(this.B);
        this.h.setAntiAlias(true);
        this.i = new Paint();
        this.i.setColor(this.C);
        this.i.setAntiAlias(true);
        this.editPreviewViewModel = this.G.get();
        EditPreviewViewModel var1;
        if ((var1 = this.editPreviewViewModel) != null) {
            if (var1.m() != null && this.editPreviewViewModel.m().getValue() != null) {
                this.l = (Integer) this.editPreviewViewModel.m().getValue();
            }

            if (this.editPreviewViewModel.n() != null && this.editPreviewViewModel.n().getValue() != null) {
                this.m = (Double) this.editPreviewViewModel.n().getValue();
            }

            if (this.editPreviewViewModel.i() != null && this.editPreviewViewModel.i().getValue() != null) {
                this.g = (Long) this.editPreviewViewModel.i().getValue();
            }

            this.a(this.g);
            if (this.editPreviewViewModel.getLdUuid() != null) {
                this.a((String) this.editPreviewViewModel.getLdUuid().getValue());
            }

            this.setOnLongClickListener((var1x) -> {
                if (var1x instanceof WaveTrackView && this.j == -1) {
                    this.editPreviewViewModel.a(true);
                }

                return false;
            });
            HAEAudioLane var2;
            if ((var2 = this.editPreviewViewModel.f()) != null) {
                this.s = var2.getEndTime();
            }
        }

    }

    public void a(String var1) {//选中效果
        this.y = var1;
        if (var1 != null) {
            if (var1.equals(this.v)) {
                this.c = true;
                this.setTranslationZ(100.0F);
                this.post(this::invalidate);
            } else if (this.c) {
                this.c = false;
                this.setTranslationZ(0.0F);
                this.post(this::invalidate);
            }
        }

    }

    public void a(int var1) {
        this.l = var1;
    }

    public void a(double var1) {
        this.m = var1;
        this.post(() -> {
            BaseTrackView var10000 = this;
            BaseTrackView var10001 = this;
            LayoutParams var10002 = this.getLayoutParams();
            BaseTrackView var10004 = this;
            int i = (int) this.j();
            var10002.width = var10004.a * 2 + i;
            var10001.setLayoutParams(var10002);
            var10000.requestLayout();
        });
    }

    public void a(long var1) {
        this.g = var1;
    }

    @SuppressLint({"ClickableViewAccessibility"})
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int var2;
        float var7;
        if ((var2 = motionEvent.getActionMasked()) != 0) {
            label89:
            {
                if (var2 != 1) {
                    if (var2 == 2) {
                        if (motionEvent.getX() <= (float) this.a) {
                            return true;
                        }

                        float var10001;
                        if ((var2 = this.j) == 0) {
                            var10001 = this.b(motionEvent);
                            this.a((int) (var10001 - this.k), this.j, motionEvent);
                            this.k = var10001;
                            return true;
                        }

                        if (var2 == 1) {
                            var10001 = var7 = this.b(motionEvent);
                            this.a((int) (this.k - var7), this.j, motionEvent);
                            this.k = var10001;
                            return true;
                        }

                        return super.onTouchEvent(motionEvent);
                    }

                    if (var2 == 5) {
                        break label89;
                    }

                    if (var2 != 6) {
                        return super.onTouchEvent(motionEvent);
                    }
                }

                if (motionEvent.getPointerCount() == 1) {
                    if ((var2 = this.j) != -1) {
                        int var10000 = var2;
                        long var6 = this.D;
                        byte var4;
                        if (var10000 == 0) {
                            var4 = 1;
                        } else {
                            var4 = -1;
                        }

                        this.a(var6, var4);
                        this.o = 0.0D;
                        this.n = 0.0D;
                        this.D = 0L;
                        this.F = -1;
                    }

                    this.j = -1;
                    this.k = 0.0F;
                }

                this.editPreviewViewModel.a(false);
                return super.onTouchEvent(motionEvent);
            }
        }

        if (this.c && motionEvent.getPointerCount() == 1) {
            boolean var9;
            if ((var7 = motionEvent.getX(0)) < (float) (this.m() + this.a) && var7 > (float) this.m()) {
//                //SmartLog.i("BaseTrackView", "checkActionLocation isTrue ");
                this.j = 0;
                var9 = true;
            } else {
                label78:
                {
                    double var8;
                    double var11 = var8 = (double) var7;
                    double var10 = (double) this.m();
                    if (var11 > this.j() + var10 + (double) this.a) {
                        var11 = var8;
                        var8 = (double) this.m();
                        if (var11 < this.j() + var8 + (double) (this.a * 2)) {
                            this.j = 1;
                            var9 = true;
                            break label78;
                        }
                    }

                    var9 = false;
                }
            }

            if (var9) {
                this.A = motionEvent.getRawX();
                this.getParent().requestDisallowInterceptTouchEvent(true);
                this.k = this.b(motionEvent);
            } else {
                this.getParent().requestDisallowInterceptTouchEvent(false);
                this.k = 0.0F;
            }
        }

        return super.onTouchEvent(motionEvent);
    }

    abstract void a(long var1, int var3);

    protected void a(int var1, int var2, MotionEvent var3) {
        double var4 = (double) (this.m() + this.a);
        var4 += this.j();
        if (this instanceof WaveTrackView || var2 != 1 || var1 >= 0 || !(var4 - (double) var1 >= this.g() + (double) this.a)) {
            if (var1 != 0 && (var1 <= 0 || !(this.j() - (double) var1 <= this.h())) && (var1 >= 0 || !(this.j() - (double) var1 >= this.g()))) {
                if (var2 != 0 || this.m() >= -var1) {
                    boolean var7;
                    if (var2 == 0) {
                        if (var1 < 0) {
                            var7 = true;
                        } else {
                            var7 = false;
                        }

                        this.n += (double) var1;
                        if (this.F > 0 && Math.abs(this.m() - this.F) < this.E) {
                            return;
                        }

                        this.F = -1;
                        this.a(var3);
                        this.D = (long) com.huawei.hms.audioeditor.ui.common.utils.a.d(com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l), com.huawei.hms.audioeditor.ui.common.utils.a.b(this.n, this.m));
                    } else {
                        if (var1 > 0) {
                            var7 = true;
                        } else {
                            var7 = false;
                        }

                        this.o += (double) var1;
                        if (this.F > 0) {
                            double var5 = (double) this.m();
                            if (Math.abs(this.j() + var5 - (double) this.F) < (double) this.E) {
                                return;
                            }
                        }

                        this.F = -1;
                        this.a(var3);
                        this.D = (long) com.huawei.hms.audioeditor.ui.common.utils.a.d(com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l), com.huawei.hms.audioeditor.ui.common.utils.a.b(this.o, this.m));
                    }

                    this.a(var7, var2);
                    this.r();
                    this.post(this::invalidate);
                }
            }
        }
    }

    public void a(boolean var1, int var2) {
        BaseTrackView var10000;
        BaseTrackView var10001;
        BaseTrackView var10002;
        double var3;
        double var5;
        double var6;
        if (var1) {
            if (var2 == 0) {
                if (this.l() + this.D < this.g) {
                    return;
                }

                if ((var3 = (double) (this.m() - this.b())) > 0.0D && var3 < (double) this.E) {
                    this.n -= var3;
                    this.D = this.g - this.l();
                    this.F = this.m();
                    this.u();
                }
            }

            if (var2 == 1) {
                if (this.c() - this.D < this.g) {
                    return;
                }

                var6 = (double) this.m();
                if ((var6 = this.j() + var6 - (double) this.b()) > 0.0D && var6 < (double) this.E) {
                    var10000 = this;
                    var10001 = this;
                    var10002 = this;
                    this.o += var6;
                    this.D = this.c() - this.g;
                    var5 = (double) this.m();
                    var10001.F = (int) (var10002.j() + var5);
                    var10000.u();
                }
            }
        } else {
            if (var2 == 0) {
                if (this.l() + this.D > this.g) {
                    return;
                }

                if ((var3 = (double) (this.b() - this.m())) > 0.0D && var3 < (double) this.E) {
                    this.n += var3;
                    this.D = this.g - this.l();
                    this.F = this.m();
                    this.u();
                }
            }

            if (var2 == 1) {
                if (this.c() - this.D > this.g) {
                    return;
                }

                if ((var6 = (double) (this.b() - this.m()) - this.j()) < (double) this.E) {
                    var10000 = this;
                    var10001 = this;
                    var10002 = this;
                    this.o -= var6;
                    this.D = this.c() - this.g;
                    var5 = (double) this.m();
                    var10001.F = (int) (var10002.j() + var5);
                    var10000.u();
                }
            }
        }

    }

    protected void a(MotionEvent var1) {
        if (this.getParent().getParent().getParent().getParent() instanceof MainHorizontalScrollView) {
            MainHorizontalScrollView var2;
            if ((var2 = (MainHorizontalScrollView) this.getParent().getParent().getParent().getParent()) != null) {
                int var10001 = com.huawei.hms.audioeditor.ui.common.utils.g.b(this.getContext());
                double var3 = (double) com.huawei.hms.audioeditor.ui.common.utils.a.a((float) var10001, 8.0F);
                double var5 = (double) var10001 - var3;
                if (com.huawei.hms.audioeditor.ui.common.utils.a.a((double) this.A, (double) var1.getRawX()) && com.huawei.hms.audioeditor.ui.common.utils.a.a(var3, (double) var1.getRawX())) {
                    var2.a((double) ((int) com.huawei.hms.audioeditor.ui.common.utils.a.a(com.huawei.hms.audioeditor.ui.common.utils.a.e((double) var1.getRawX(), var3), 0)));
                } else if (com.huawei.hms.audioeditor.ui.common.utils.a.a((double) var1.getRawX(), (double) this.A) && com.huawei.hms.audioeditor.ui.common.utils.a.a((double) var1.getRawX(), var5)) {
                    var2.a((double) ((int) com.huawei.hms.audioeditor.ui.common.utils.a.a(com.huawei.hms.audioeditor.ui.common.utils.a.e((double) var1.getRawX(), var5), 0)));
                } else {
//                    //SmartLog.i("xxxxxx", com.huawei.hms.audioeditor.ui.p.a.a("else: startScrollX:event.getRawX():").append(var1.getRawX()).toString());
                }

            }
        }
    }

    public int b() {
        return (int) com.huawei.hms.audioeditor.ui.common.utils.a.d(com.huawei.hms.audioeditor.ui.common.utils.a.b((double) this.g, com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l)), this.m);
    }

    protected double h() {
        return com.huawei.hms.audioeditor.ui.common.utils.a.d(com.huawei.hms.audioeditor.ui.common.utils.a.b(this.p, com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l)), this.m);
    }

    protected double g() {
        return (double) this.f() / com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l) * this.m;
    }

    public void b(double var1) {
        this.z = Util.toScale(var1, 0);
        this.postInvalidate();
    }

    public long l() {
        return this.startTime;
    }

    public long c() {
        HAEAsset var1;
        if ((var1 = this.x) != null) {
            return var1.getEndTime();
        } else {
            HAEEffect var2;
            return (var2 = this.w) != null ? var2.getEndTime() : 0L;
        }
    }

    public int m() {
        return (int) (com.huawei.hms.audioeditor.ui.common.utils.a.d(com.huawei.hms.audioeditor.ui.common.utils.a.b((double) this.startTime, com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l)), this.m) + this.n + this.z);
    }

    public double j() {
        return (double) this.i() / com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l) * this.m - this.n - this.o - (double) this.f;
    }

    public long k() {
        double var1 = (double) this.H / this.m;
        return (long) (com.huawei.hms.audioeditor.ui.common.utils.d.a(this.l) * var1);
    }

    public int d() {
        return this.l;
    }

    public double e() {
        return this.m;
    }

    public long i() {
        return this.r;
    }

    public long f() {
        return this.s;
    }

    public void c(double var1) {
        this.p = var1;
    }

    public void b(long var1) {
        this.s = var1;
        //SmartLog.i("cutDuration", com.huawei.hms.audioeditor.ui.p.a.a("maxDuration ").append(this.s).toString());
    }

    public EditPreviewViewModel n() {
        return this.editPreviewViewModel;
    }

    public String o() {
        return this.v;
    }

    public void b(String var1) {
        this.v = var1;
    }

    public HAEAsset a() {
        return this.x;
    }

    public void a(HAEAsset var1) {
        this.x = var1;
        this.r();
    }

    public void a(HAEEffect var1) {
        this.w = var1;
        this.r();
    }

    public void s() {
        String var1;
        if ((var1 = this.y) != null && var1.equals(this.v)) {
            this.c = true;
            this.setTranslationZ(100.0F);
            this.post(this::invalidate);
        }

    }

    public void r() {
        this.z = 0.0D;
        LayoutParams var1;
        if ((var1 = this.getLayoutParams()) != null) {
            LayoutParams var10001 = var1;
            LayoutParams var10002 = var1;
            double var3 = (double) this.m();
            var10002.width = (int) (this.j() + var3 + (double) (this.a * 2));
            this.setLayoutParams(var10001);
        }
    }

    public boolean p() {
        return this.j != -1;
    }

    public int a(String var1, Paint var2) {
        if (TextUtils.isEmpty(var1)) {
            return 0;
        } else if (var2 == null) {
            return 0;
        } else {
            Rect var3;
            var3 = new Rect();
            var2.getTextBounds(var1, 0, var1.length(), var3);
            return Math.abs(var3.width());
        }
    }
}
